using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class Delete : Operator
    {
        // DML Delete statement

        #region Fields
        string table;
        public string Table
        {
            get { return table; }
            set { table = value; }
        }

        List<string> columns;
        public List<string> Columns
        {
            get { return columns; }
            set { columns = value; }
        }
        
        List<Condition> conditions;
        public List<Condition> Conditions
        {
            get { return conditions; }
            set { conditions = value; }
        }
        #endregion Fields

        public Delete(string table)
        {
            this.table = table;
            this.columns = new List<string>();
            this.conditions = new List<Condition>();
            this.parent = null;
        }

        public Delete(string table, IEnumerable<string> columns, IEnumerable<Condition> conditions)
        {
            this.table = table;
            this.columns = new List<string>(columns);
            this.conditions = new List<Condition>(conditions);
            this.parent = null;

            // Dummy check
            if (this.columns.Count != this.conditions.Count)
                throw new Exception("Number of columns must equal the number of conditions.");
            
            // Check all of the conditions are equality conditions
            foreach (Condition c in this.conditions)
            {
                if (c.CondType != ConditionType.EQ)
                    throw new Exception("Found a condition of type " + c.CondType.ToString() + " in a delete statement - only equality conditions are accepted.");
            }
        }

        public void AddCondition(string column, Condition condition)
        {
            if (condition.CondType != ConditionType.EQ)
                throw new Exception("Found a condition of type " + condition.CondType.ToString() + " in a delete statement - only equality conditions are accepted.");
            this.columns.Add(column);
            this.conditions.Add(condition);
        }

        public override void NotifyChange(Operator old, Operator newOp)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool isQuery
        {
            get
            {
                return false;
            }
        }
        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }
        public override Operator Clone()
        {
            return new Delete(this.table, this.columns, this.conditions);
        }

        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }

    }
}
